package com.gmrz.uaf.policy.verification;
 
 import com.gmrz.uaf.protocol.v1.processor.exception.UAFErrorCode;
 import com.gmrz.uaf.protocol.v1.schema.AuthenticationAssertion;
 import com.gmrz.uaf.protocol.v1.schema.AuthenticatorSpec;
 import com.gmrz.uaf.protocol.v1.schema.Policy;
 import com.gmrz.uaf.protocol.v1.schema.RegistrationAssertion;
 import java.util.List;
 import java.util.Map;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
 public class PolicyVerificationProcessor
 {
   private static final Logger LOG = LogManager.getLogger(PolicyVerificationProcessor.class);
 
   private static IPolicyVerifier<RegistrationAssertion> registrationPolicyVerifier = new RegistrationPolicyVerifier();
   private static IPolicyVerifier<AuthenticationAssertion> authenticationPolicyVerifier = new AuthenticationPolicyVerifier();
 
   private static int maxNumberOfAssertions = 5;
 
   public static List<RegistrationAssertion> verifyRegistrationPolicy(List<RegistrationAssertion> assertions, Map<String, AuthenticatorSpec> authenticatorSpecs, Policy policy)
     throws PolicyVerificationException
   {
     try
     {
       LOG.debug("Policy verification for registration .");
       if (registrationPolicyVerifier == null) {
         throw new PolicyVerificationException(UAFErrorCode.SYS_POLICY_VERIFICATION_FAILED, "RegistrationPolicyVerifier is NULL");
       }
 
       PolicyVerificationUtil.verifyInputs(assertions, authenticatorSpecs, policy);
 
       checkAllowedAssertionsSize(assertions.size());
 
       registrationPolicyVerifier.filterDisallowedAssertions(assertions, authenticatorSpecs, policy);
 
       return registrationPolicyVerifier.selectAcceptedAssertions(assertions, authenticatorSpecs, policy);
     }
     catch (PolicyVerificationException pve) {
       LOG.error(" Error occurred during registration policy verification : {} ", pve.getMessage());
       throw new PolicyVerificationException(UAFErrorCode.SYS_POLICY_VERIFICATION_FAILED, "Failed during policy verification for registration");
     }
     catch (Exception e) {
    	 LOG.error(e);
       LOG.error(" Unknown Error occurred during registration policy verification : {} ", e.getMessage());
     }throw new PolicyVerificationException(UAFErrorCode.SYS_POLICY_VERIFICATION_FAILED, "Failed during registration policy verification");
   }
 
   public static List<AuthenticationAssertion> verifyAuthenticationPolicy(List<AuthenticationAssertion> assertions, Map<String, AuthenticatorSpec> authenticatorSpecs, Policy policy)
     throws PolicyVerificationException
   {
     try
     {
       LOG.debug("Policy verification for authentication . ");
 
       if (authenticationPolicyVerifier == null) {
         throw new PolicyVerificationException(UAFErrorCode.SYS_POLICY_VERIFICATION_FAILED, "AuthenticationPolicyVerifier is NULL");
       }
 
       PolicyVerificationUtil.verifyInputs(assertions, authenticatorSpecs, policy);
 
       checkAllowedAssertionsSize(assertions.size());
 
       authenticationPolicyVerifier.filterDisallowedAssertions(assertions, authenticatorSpecs, policy);
 
       return authenticationPolicyVerifier.selectAcceptedAssertions(assertions, authenticatorSpecs, policy);
     }
     catch (PolicyVerificationException pve) {
       LOG.error(" Error occurred during authentication policy verification : {}", pve.getMessage());
       throw new PolicyVerificationException(UAFErrorCode.SYS_POLICY_VERIFICATION_FAILED, "Failed during policy verification for authentication");
     }
     catch (Exception e) {
       LOG.error(" Unknown Error occurred authentication during policy verification : {}", e.getMessage());
     }throw new PolicyVerificationException(UAFErrorCode.SYS_POLICY_VERIFICATION_FAILED, "Failed during authentication policy verification");
   }
 
   private static void checkAllowedAssertionsSize(int assertionListSize) throws PolicyVerificationException
   {
     if (assertionListSize > getMaxAssertionsAllowed()) {
       LOG.error("Number of Assertions : {}, are more than max allowed : {}", Integer.valueOf(assertionListSize), Integer.valueOf(getMaxAssertionsAllowed()));
       throw new PolicyVerificationException(UAFErrorCode.SYS_POLICY_VERIFICATION_FAILED, "Assertions do not match the configured value");
     }
   }
 
   private static int getMaxAssertionsAllowed() throws PolicyVerificationException {
     return maxNumberOfAssertions;
   }
 }
